spring无法转换“java”类型的值。lang.String“到所需类型”java。提交表单时lang.Long'错误
我在控制器中有一个:
@RequestMapping(
value = "/update",
params = {"new_profile_image"},
method = RequestMethod.POST)
public ModelAndView updateUserProfileImage(
@RequestParam(value = "new_profile_image") CommonsMultipartFile newProfileImage,
ModelMap model) {
System.out.println("controller executed!");
if(newProfileImage != null && !newProfileImage.isEmpty()) {
updateService.updateUserProfileImage(newProfileImage.getBytes());
}
return new ModelAndView("redirect:/users/my_profile");
}
在jsp文件中:
<form action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />" method="post" enctype="multipart/form-data">
<input name="new_profile_image" type="file" id="new_profile_image">
<button type="submit" class="btn btn-primary">Update</button>
</form>
当我提交一张图片时,我得到了
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Feb 08 19:08:10 CST 2020 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "update"
字符串"controller executed!"
永远不会出现在控制台中。我在RequestMapping注释中将value = "/update"
更改为value = "/updateImage"
,将action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />"
更改为action="<c:url value='/users/updateImage?${_csrf.parameterName}=${_csrf.token}' />"
,错误消息更改为nested exception is java.lang.NumberFormatException: For input string: "updateImage"
我不知道出了什么问题,在eclipse的控制台中也没有显示异常
编辑:
我忘了说控制器在类级别有RequestMapping("/users")
现在我在控制器value = "/update"
中更改了value = "/updateImage"
,但我在jsp页面中保留了action="<c:url value='/users/update?${_csrf.parameterName}=${_csrf.token}' />"
,错误仍然是:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Feb 08 19:08:10 CST 2020 There was an unexpected error (type=Bad Request, status=400). Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "update"
我猜请求甚至没有到达控制器
# 1 楼答案
最后,我通过从
@RequestMapping
中删除params
属性来解决这个问题。因此,控制器最终为:我在其他方法中将
params
属性与post请求一起使用,它可以工作,我想这里的问题与请求是多部分请求有关。但我真的不知道实际的问题是什么